import 'package:casdon/core/utils/string_externtion.dart';
import 'package:casdon/modules/recipe/data/model/recipe_placeholder_data.dart';
import 'package:json_annotation/json_annotation.dart';

part 'cook_module_content_api.g.dart';

// 请求参数模型
@JsonSerializable()
class RequestCookModuleContentApi {
  final String? moduleId;
  final String? cookClassesModuleId;
  final PageQuery pageQuery;

  RequestCookModuleContentApi({
    required this.moduleId,
    required this.cookClassesModuleId,
    required this.pageQuery,
  });

  factory RequestCookModuleContentApi.fromJson(Map<String, dynamic> json) =>
      _$RequestCookModuleContentApiFromJson(json);

  Map<String, dynamic> toJson() {
    final jsonMap = _$RequestCookModuleContentApiToJson(this);
    jsonMap['pageQuery'] = pageQuery.toJson(); // 手动确保pageQuery被转换成Map
    return jsonMap;
  }
}

// 分页查询参数
@JsonSerializable()
class PageQuery {
  final int pageNum;
  final int pageSize;

  PageQuery({required this.pageNum, required this.pageSize});

  factory PageQuery.fromJson(Map<String, dynamic> json) =>
      _$PageQueryFromJson(json);

  Map<String, dynamic> toJson() => _$PageQueryToJson(this);
}

// 响应模型
@JsonSerializable()
class ResponseCookModuleContentApi {
  final int total;
  final List<CookModuleContent> rows;

  ResponseCookModuleContentApi(this.total, this.rows);

  factory ResponseCookModuleContentApi.fromJson(Map<String, dynamic> json) =>
      _$ResponseCookModuleContentApiFromJson(json);

  Map<String, dynamic> toJson() => _$ResponseCookModuleContentApiToJson(this);
}

// 菜谱内容模型
@JsonSerializable()
class CookModuleContent {
  @JsonKey(name: 'viewOssUrl', fromJson: _getImageUrl, toJson: _imageUrlToJson)
  final String? imageUrl;
  final String? name;
  @JsonKey(
    fromJson: _getVideoUrl,
    toJson: _videoUrlToJson,
    readValue: _readVideoUrl,
  )
  final String? url; // 视频链接
  final String id;

  final String cookClassesModuleId;

  final String? view;
  final String? collect;
  final String? count;

  CookModuleContent({
    this.imageUrl,
    this.name,
    this.url,
    required this.id,
    required this.cookClassesModuleId,
    required this.view,
    required this.collect,
    required this.count,
  });

  factory CookModuleContent.fromJson(Map<String, dynamic> json) =>
      _$CookModuleContentFromJson(json);

  Map<String, dynamic> toJson() => _$CookModuleContentToJson(this);

  // 自定义转换函数
  static String? _getImageUrl(dynamic value) {
    if (value is String && value.isNotEmpty) {
      return value.split(",").first;
    }

    return recipePlaceHolderImgUrl;
  }

  // 反向序列化方法
  static dynamic _imageUrlToJson(String? url) => url;

  // 自定义转换函数
  static String? _getVideoUrl(Map<dynamic, dynamic>? json) {
    if (json == null) return null;
    final Map<String, dynamic> safeJson = Map<String, dynamic>.from(json);
    if (safeJson['url'] != null && safeJson['url'] is String) {
      String url = safeJson['url'];
      if (url.isNotEmpty) return url;
    }

    if (safeJson['ossUrls'] != null && safeJson['ossUrls'] is String) {
      String ossUrls = safeJson['ossUrls'];
      if (ossUrls.isNotEmpty) return ossUrls.split(',').first;
    }

    return null;
  }

  static Map<String, dynamic> _readVideoUrl(
    Map<dynamic, dynamic> json,
    String key,
  ) => {'url': json['url'], 'ossUrls': json['ossUrls']};

  // 反向序列化方法
  static dynamic _videoUrlToJson(String? url) => url;
}
